﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BO;
using Entity;

namespace Final_Prototype_UsingDev_QuanLy_071012
{
    public partial class Form_Custom_Authorized : Form
    {

        CustomerBO csBO = new CustomerBO();
        StaffBO sBO = new StaffBO();
        CustomerEntity csEntity = new CustomerEntity();
        public delegate void delPassData(string sData);     

        public Form_Custom_Authorized()
        {
            InitializeComponent();
        }

        private void grd1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void grdCustomer_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        /// <summary>
        /// load danh sach khach hang
        /// </summary>
        public void LoadCustomer()
        {
            grdCustomerFrom.DataSource = csBO.LoadCustomer();
            for (int i = 10; i<grdCustomerFrom.Columns.Count; i++)
                grdCustomerFrom.Columns[i].Visible = false;
        }

        /// <summary>
        /// Loc danh sach khach hang theo sale name
        /// </summary>
        public void LoadCustomerFromSaleName()
        {
            string FromSaleName = cboChooseSaleMan.Text;
            if (FromSaleName != "")
                grdCustomerFrom.DataSource = csBO.LoadCustomerBySaleName(FromSaleName);
            else
            {
                grdCustomerFrom.DataSource = csBO.LoadCustomer();
                grpFromSale.Text = "All customer";
            }
            for (int i = 1; i < grdCustomerFrom.Columns.Count; i++)
                grdCustomerFrom.Columns[i].Visible = false;
            grdCustomerFrom.Columns[2].Visible = true;
            grdCustomerFrom.Columns[8].Visible = true;
            grdCustomerFrom.Columns[9].Visible = true;


        }

        /// <summary>
        /// Loc danh sach khach hang theo sale name
        /// </summary>
        public void LoadCustomerToSaleName()
        {

            string ToSaleName = cboToSaleMan.Text;
            grdCustomerTo.DataSource = csBO.LoadCustomerBySaleName(ToSaleName);

            for (int i = 0; i < grdCustomerTo.Columns.Count; i++)
                grdCustomerTo.Columns[i].Visible = false;
            grdCustomerTo.Columns[1].Visible = true;
            grdCustomerTo.Columns[7].Visible = true;
            grdCustomerTo.Columns[8].Visible = true;
        }

        /// <summary>
        /// load danh sach sale name duoc chon
        /// </summary>
        private void LoadFromSaleName()
        {
            cboChooseSaleMan.DataSource = sBO.LoadStaff();
            cboChooseSaleMan.DisplayMember = "StaffName";
            cboChooseSaleMan.SelectedIndex = -1;

        }

        /// <summary>
        /// load danh sach sale name duoc uy quyen
        /// </summary>
        public void LoadToSaleName()
        {
            string SaleName = cboChooseSaleMan.Text.ToString();
            cboToSaleMan.DataSource = sBO.LoadStaff(SaleName);
            cboToSaleMan.DisplayMember = "StaffName";
            cboToSaleMan.SelectedIndex = -1;

        }

        /// <summary>
        /// tim kiem khach hang
        /// </summary>
        private void FindCustomer()
        {
            grdCustomerFrom.DataSource = csBO.FindCustomer(txtFind.Text.ToString());
        }

        /// <summary>
        /// Save thong tin khach hang
        /// </summary>
        private void EditAuthorize()
        {         
            string ToSale = cboToSaleMan.Text;
            string FromSale, CustomerCode;
            DateTime FromDate =dpFrom.Value;
            DateTime ToDate=dpTo.Value;
            foreach (DataGridViewRow row in grdCustomerFrom.Rows)
            {
                if (row.Cells[0].Value != null)          
                {
                    int index = row.Index;
                    FromSale = grdCustomerFrom.Rows[index].Cells["StaffName"].Value.ToString();
                    CustomerCode = grdCustomerFrom.Rows[index].Cells["CustomerCode"].Value.ToString();
                    csBO.EditAuthorize(CustomerCode, FromSale, ToSale, FromDate, ToDate);
                }
            }

            MessageBox.Show("Authorized successful!");
        }

        private void Form_Custom_Authorized_Load(object sender, EventArgs e)
        {          
            LoadFromSaleName();
            LoadToSaleName();
        }

        private void txtFind_TextChanged(object sender, EventArgs e)
        {
            FindCustomer();
            string Find = txtFind.Text;
            cboChooseSaleMan.Text = null;
            if (Find == "")
            {
                grpFromSale.Text = "All customer";
            }
            else
            {
                grpFromSale.Text = "Find " + txtFind.Text;
            }
        }

        private void btnSetAuthorized_Click(object sender, EventArgs e)
        {
            EditAuthorize();
            LoadCustomerFromSaleName();
            LoadCustomerToSaleName();
        }

        private void cboChooseSaleMan_SelectedIndexChanged(object sender, EventArgs e)
        {
            string SaleName = cboChooseSaleMan.Text;
            LoadCustomerFromSaleName();
            LoadToSaleName();
            grpFromSale.Text = SaleName;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void cboToSaleMan_SelectedIndexChanged(object sender, EventArgs e)
        {
            string SaleName = cboToSaleMan.Text;
            LoadCustomerToSaleName();
            grpToSale.Text = SaleName;

        }

        private void grdCustomerFrom_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Form_Custom_Management_Edit fr2 = new Form_Custom_Management_Edit(this);
            string sFormName;
            foreach (DataGridViewRow row in grdCustomerFrom.Rows)
            {
                if (row.Selected == true)
                {
                    int index = row.Index;
                    sFormName = grdCustomerFrom.Rows[index].Cells["CustomerCode"].Value.ToString();
                    fr2.Text = sFormName;
                    delPassData del = new delPassData(fr2.FillInfo);
                    del(sFormName);
                }
            }
            fr2.Show();
        }
    }
}
